package com.suning.sawp.web.controller;

import java.security.Principal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 
 * ���ƿͻ���ҵ�������<br> 
 * ��������ϸ������
 *
 * @author 12061818
 * @see [�����/����]����ѡ��
 * @since [��Ʒ/ģ��汾] ����ѡ��
 */

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.RegAndBindErrCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.MainPushTaskDetailResponse;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.exception.SawpException;
import com.suning.sawp.intf.mainpushnew.MainPushClientService;
import com.suning.sawp.intf.mainpushnew.MainPushQueryService;
import com.suning.sawp.intf.mainpushnew.StaffMainPushService;
import com.suning.sawp.intf.model.ModelErrorConstant;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.vo.mainpushnew.MainPushStaffVo;
import com.suning.sawp.vo.mainpushnew.MainPushStaffidVo;
import com.suning.sawp.vo.mainpushnew.MainPushStoreVo;
import com.suning.sawp.vo.mainpushnew.StaffAssginDetailVo;
import com.suning.sawp.vo.mainpushnew.StaffAssginParam;
import com.suning.sawp.web.exception.MyGsonView;
@Controller
@RequestMapping("/mainPush")
public class MainPushController extends DjBaseController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(MainPushController.class);
    
    @Resource
    StaffMainPushService staffMainPushService;
    
    /**
     * 主推服务
     */
    @Autowired
    MainPushClientService mainPushClientService;
    
    
    @Autowired
   MainPushQueryService mainPushQueryService;

   @Autowired
   StoreManService storeManService;
   /**
    * 
    * 功能描述: 某一款数的店员分配列表<br>
    * 〈功能详细描述〉
    *
    * @return
    * @see [相关类/方法](可选)
    * @since [产品/模块版本](可选)
    */
    @RequestMapping("/querySaffAssginList.do")
    public MyGsonView querySaffAssginList(Long modelId, Long storeTaskId) {
        LOGGER.info("querySaffAssginList.do, modelId:{}, storeTaskId:{}", modelId, storeTaskId);
        ReturnMsg<List<StaffAssginDetailVo>> retMsg = new ReturnMsg<List<StaffAssginDetailVo>>();
        try {
            List<StaffAssginDetailVo> list = staffMainPushService.querySaffAssginList(storeTaskId, modelId);
            retMsg.setData(list);
        } catch (Exception e) {
            throw new SawpException("��ѯ�������ĵ�Ա�б��쳣", e);
        }
        return setGsonView(retMsg);
    }
    
    /**
     * 
     * 功能描述: 某一款数分配店员任务<br>
     * 〈功能详细描述〉
     *
     * @param param
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/assignStaffTask.do")
    public MyGsonView assignStaffTask(@RequestBody StaffAssginParam param) {
        LOGGER.info("assignStaffTask.do, params:{}", param);
        ReturnMsg<String> retMsg = staffMainPushService.assignStaffTask(param);
        return setGsonView(retMsg);
    }
    
    /**
     * 查询主推款式明细
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param storeTaskId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryModelDetail.do")
    public MyGsonView queryModelDetail(Principal principal, String storeTaskId) {
        LOGGER.info("queryModelDetail.do, storeTaskId:{},staffId:{}", storeTaskId, principal.getName());
        ReturnMsg<MainPushTaskDetailResponse> retMsg = new ReturnMsg<MainPushTaskDetailResponse>();
        if (StringUtils.isBlank(storeTaskId)) {
            retMsg.setError(ModelErrorConstant.MODEL_ERR_E001, "���storeTaskId����");
        } else if (!mainPushClientService.isExistTask(storeTaskId)) {
            retMsg.setError(ModelErrorConstant.MODEL_ERR_E001, "���������񲻴���,��ȷ��");
        } else {
            try {
                // 查询主推详情接口
                MainPushTaskDetailResponse resp = mainPushClientService.queryMainPushTaskInfo(storeTaskId, 
                        principal.getName());
                // 设置返回数据
                retMsg.setData(resp);
            } catch (Exception e) {
                // 异常处理
                retMsg.setRetFlag(ReturnMsg.FAIL);
                retMsg.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E999);
                retMsg.setErrorMessage(
                        RegAndBindErrCodeConstants.COMMON_ERR_MAP.get(RegAndBindErrCodeConstants.COMMON_ERR_E999));
                LOGGER.error("getIndexUrl,staffId:" + principal.getName(), e);
            }
        }
        return setGsonView(retMsg);
    }
	
    @RequestMapping("/queryMainPushForStore.do")
    public MyGsonView queryMainPushForStore(Principal principal, String cateCode, String month) {
        LOGGER.info("queryMainPushForStore enter,staffId:{},cateCode:{},month:{}", principal.getName(), cateCode, month);
        ReturnMsg<MainPushStoreVo> result = new ReturnMsg<MainPushStoreVo>();
        if (StringUtils.isNotBlank(cateCode)) {
            try {
                StoreManDto storeManDto = storeManService.queryStoreManAttachInfo(principal.getName());
                result = mainPushQueryService.queryMainPushForStore(cateCode, month, storeManDto.getStoreCode());
            } catch (Exception e) {
                LOGGER.error("queryMainPushForStore fail", e);
                result.setError(ErrorCodeConstants.ERR_0002);
                return setGsonView(result);
            }
        } else {
            result.setError(ErrorCodeConstants.ERR_0001);
        }
        LOGGER.debug("queryMainPushForStore OUT");
        return setGsonView(result);
    }

    @RequestMapping("/queryMainPushDetailForStore.do")
    public MyGsonView queryMainPushDetailForStore(Principal principal, String modelId, String queryParm,
            String queryType, String storeTaskId) {
        LOGGER.info("queryMainPushDetailForStore enter,staffId:{},modelId:{},queryType:{},queryParm:{},storeTaskId:{}",
                principal.getName(), modelId, queryType, queryParm, storeTaskId);
        ReturnMsg<MainPushStaffidVo> result = new ReturnMsg<MainPushStaffidVo>();
        if (StringUtils.isNotBlank(modelId) && StringUtils.isNotBlank(queryType) && StringUtils.isNotBlank(storeTaskId)) {
            try {
                StoreManDto storeManDto = storeManService.queryStoreManAttachInfo(principal.getName());
                result = mainPushQueryService.queryMainPushDetailForStore(modelId, queryParm, queryType, storeManDto,
                        storeTaskId);
            } catch (Exception e) {
                LOGGER.error("queryMainPushDetailForStore fail", e);
                result.setError(ErrorCodeConstants.ERR_0002);
                return setGsonView(result);
            }
        } else {
            result.setError(ErrorCodeConstants.ERR_0001);
        }
        LOGGER.debug("queryMainPushDetailForStore OUT");
        return setGsonView(result);
    }

    @RequestMapping("/queryMainPushForStaff.do")
    public MyGsonView queryMainPushForStaff(Principal principal, String cateCode, String month) {
        LOGGER.info("queryMainPushForStaff enter,staffId:{},cateCode:{},month:{}", principal.getName(), cateCode, month);
        ReturnMsg<MainPushStaffVo> result = new ReturnMsg<MainPushStaffVo>();
        if (StringUtils.isNotBlank(cateCode)) {
            try {
                StoreManDto storeManDto = storeManService.queryStoreManAttachInfo(principal.getName());
                result = mainPushQueryService.queryMainPushForStaff(cateCode, month, storeManDto);
            } catch (Exception e) {
                LOGGER.error("queryMainPushDetailForStore fail", e);
                result.setError(ErrorCodeConstants.ERR_0002);
                return setGsonView(result);
            }
        } else {
            result.setError(ErrorCodeConstants.ERR_0001);
        }
        LOGGER.debug("queryMainPushDetailForStore OUT");
        return setGsonView(result);
    }
}
